<?php
class Student extends CI_Controller{
	function __construct(){
		parent::__construct();
	}
	
	//student enroll
	function enroll(){
		//if not logged_in redirect to login page
		if ($this->session->userdata('logged_in')==false){
			redirect('/login');
		}
		
		$this->load->model('liststudent');
		//if student already enrolled one topic
		if ($this->liststudent->checkEnroll()){
			$this->load->view('student/enrollSuccess');
			return;
		}
		
		//else load enroll form
		$data['title'] = 'Register form';
		$this->load->library('form_validation');
		$config = array(
			array(
				'field' => 'mark',
				'rules' => 'required|trim'
			),
			array(
				'field' => 'trainingmark',
				'rules' => 'required|trim'
			),
			array(
				'field' => 'achievement',
				'rules' => 'required|trim'
			),
			array(
				'field' => 'class',
				'rules' => 'required|trim'
			)
		);
		$this->form_validation->set_rules($config);
		$this->form_validation->set_message('Please try again.');
		
		if ($this->form_validation->run()){
			$data['title'] = 'Success';
			$this->load->view('student/enrollSuccess',$data);
			$this->load->model('liststudent');
			$this->liststudent->add();	
			$this->load->model('thesismodel');
			//when student enroll, update quantity of student waiting
			$this->thesismodel->updateQuantity(-1,1);	
		}
		else
			$this->load->view('student/enrollForm',$data);
	}
	
	//student quit
	function quit(){
		if ($this->session->userdata('logged_in')==false){
			redirect('/login');
		}
		
		$this->load->model('liststudent');
		$this->load->model('thesismodel');
		$userId = $this->session->userdata('user_id');
		$status = $this->liststudent->checkStatus($userId);
		$this->thesismodel->updateQuantity(0,$status);
		$this->liststudent->delete();
		$this->session->set_userdata('status',1);
		//after quit redirect to site
		redirect('/site');
	}
	
	//view student profile
	function viewdetail(){
		if ($this->session->userdata('logged_in')==false){
			redirect('/login');
		}
		
		$studentId = $this->session->userdata('studentId');
		$this->load->model('usermodel');
		$data['rows'] = $this->usermodel->getProfile($studentId);
		$data['title'] = 'Profile';
		$this->load->model('liststudent');
		$data['status'] = $this->liststudent->checkStatus($studentId);
		$this->load->view('student/viewdetail',$data);
	}
	
	function registeredTopic(){
		$this->load->model('thesismodel');
		$studentId = $this->session->userdata('user_id');
		$data['rows'] = $this->thesismodel->registeredTopic($studentId);
		$data['title'] = 'Registered Topic';
		$this->load->view('student/registeredTopic',$data);
	}
}
?>